﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lib.Bases;
using Lib.Utils;

namespace MS.INV.Prices
{
    public partial class Price_ByLocation : Lib.Bases.Forms.BaseForm
    {
        SysObj.Bg_KhuVuc _cls = new SysObj.Bg_KhuVuc();

        public Price_ByLocation()
        {
            InitializeComponent();
            this.TopLevel = false;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

            dtNgay.EditValue = DateTime.Now;
        }

        private void Price_ByLocation_Load(object sender, EventArgs e)
        {
            try
            {
                _cls.LoadData();
                if (_cls.bool_TuNgay)
                {
                    rdTuNgay.Checked = true;
                    dtNgay.EditValue = _cls.TuNgay;
                }
                else
                {
                    rdKhoangTG.Checked = true;
                    dtTuNgay.EditValue = _cls.TuNgay;
                    dtDenNgay.EditValue = _cls.DenNgay;
                }

                if (_cls.bool_DoiTuong < 2)
                {
                    rdObjAll.Checked = true;
                    rdObjAll_CheckedChanged(rdObjAll, e);
                }
                else
                {
                    rdObjChoose.Checked = true;
                    rdObjChoose_CheckedChanged(rdObjChoose, e);
                }

                if (_cls.bool_GiaThucDon < 2)
                {
                    rdPriceAll.Checked = true;
                    rdPriceAll_CheckedChanged(rdPriceAll, e);

                    if (_cls.GiaKhuVuc_PT > 0 || (_cls.GiaKhuVuc_PT == 0 && _cls.GiaKhuVuc_Tien == 0))
                    {
                        rdPercent.Checked = true;
                        nuPercent.EditValue = _cls.GiaKhuVuc_PT;
                        rdPercent_CheckedChanged(rdPercent, e);
                    }
                    else
                    {
                        rdMoney.Checked = true;
                        nuMoney.EditValue = _cls.GiaKhuVuc_Tien;
                        rdMoney_CheckedChanged(rdMoney, e);
                    }
                }
                else
                {
                    rdPriceChoose.Checked = true;
                    rdPriceChoose_CheckedChanged(rdPriceChoose, e);

                    if (_cls.bool_GiaThucDon_Loai < 2)
                    {
                        rdGroup.Checked = true;
                        rdGroup_CheckedChanged(rdGroup, e);
                    }
                    else
                    {
                        rdItem.Checked = true;
                        rdItem_CheckedChanged(rdItem, e);
                    }
                }
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "FormLoad");
            }
        }

        private void rdObjAll_CheckedChanged(object sender, EventArgs e)
        {
            if (rdObjAll.Checked)
            {
                us_tables.Visible = false;
                us_locations.Visible = true;
                us_locations.SetData(_cls.Location_LoadByLocation());
            }
        }

        private void rdObjChoose_CheckedChanged(object sender, EventArgs e)
        {
            if (rdObjChoose.Checked)
            {
                us_tables.Visible = true;
                us_locations.Visible = false;
                us_tables.SetData(_cls.Location_LoadByTable());
            }
        }

        private void rdTuNgay_CheckedChanged(object sender, EventArgs e)
        {
            if (rdTuNgay.Checked)
            {
                dtNgay.Enabled = true;
                dtTuNgay.Enabled = false;
                dtDenNgay.Enabled = false;
            }
        }

        private void rdKhoangTG_CheckedChanged(object sender, EventArgs e)
        {
            if (rdKhoangTG.Checked)
            {
                dtNgay.Enabled = false;
                dtTuNgay.Enabled = true;
                dtDenNgay.Enabled = true;
            }
        }

        private void rdPriceAll_CheckedChanged(object sender, EventArgs e)
        {
            if (rdPriceAll.Checked)
            {
                pnlAll.Enabled = true;
                pnlDetail.Enabled = false;
                pnlGrItm.Enabled = false;
            }
        }

        private void rdPriceChoose_CheckedChanged(object sender, EventArgs e)
        {
            if (rdPriceChoose.Checked)
            {
                pnlAll.Enabled = false;
                pnlDetail.Enabled = true;
                pnlGrItm.Enabled = true;

                nuPercent.EditValue = 0;
                nuMoney.EditValue = 0;
            }
        }

        private void rdGroup_CheckedChanged(object sender, EventArgs e)
        {
            if (rdGroup.Checked)
            {
                grid.Visible = true;
                l_Items1.Visible = false;
                grid.DataSource = _cls.Price_LoadByGroup();
            }
        }

        private void rdItem_CheckedChanged(object sender, EventArgs e)
        {
            if (rdItem.Checked)
            {
                grid.Visible = false;
                l_Items1.Visible = true;
                l_Items1.SetData(_cls.Price_LoadByMenu());
            }
        }

        private void repRemove_Click(object sender, EventArgs e)
        {
            DataRow row = view.GetFocusedDataRow();
            if (!object.Equals(row, null))
            {
                if (Msg.YesNoWarn("Bạn có chắc muốn xóa dữ liệu này không ?") == DialogResult.Yes)
                    row.Delete();
            }
        }

        private void rdPercent_CheckedChanged(object sender, EventArgs e)
        {
            if (rdPercent.Checked)
            {
                nuPercent.Enabled = true;
                nuMoney.EditValue = 0;
                nuMoney.Enabled = false;
                lbl1.Enabled = true;
                lbl2.Enabled = false;
            }
        }

        private void rdMoney_CheckedChanged(object sender, EventArgs e)
        {
            if (rdMoney.Checked)
            {
                nuMoney.Enabled = true;
                nuPercent.EditValue = 0;
                nuPercent.Enabled = false;
                lbl1.Enabled = false;
                lbl2.Enabled = true;
            }
        }


        public void OnUpdate()
        {
            _cls.TuNgay = DateTime.Now;
            _cls.DenNgay = DateTime.Now;

            if (rdTuNgay.Checked)
            {
                _cls.bool_TuNgay = true;
                _cls.TuNgay = Convert.ToDateTime(dtNgay.EditValue);
            }
            else if (rdKhoangTG.Checked)
            {
                _cls.bool_TuNgay = false;
                _cls.TuNgay = Convert.ToDateTime(dtTuNgay.EditValue);
                _cls.DenNgay = Convert.ToDateTime(dtDenNgay.EditValue);
            }

            if (rdObjAll.Checked)
            {
                _cls.bool_DoiTuong = 1;
                _cls.Locations = us_locations.GetChecked();
            }
            else
            {
                _cls.bool_DoiTuong = 2;
                _cls.Locations = us_tables.GetData();
            }

            _cls.GiaKhuVuc_Tien = 0;
            _cls.GiaKhuVuc_PT = 0;

            if (rdPriceAll.Checked)
            {
                _cls.bool_GiaThucDon = 1;
                if (rdPercent.Checked)
                    _cls.GiaKhuVuc_PT = Cvert.ToInt32(nuPercent.EditValue, 0);
                else
                    _cls.GiaKhuVuc_Tien = Cvert.ToDecimal(nuMoney.EditValue, 0);
            }
            else
            {
                _cls.bool_GiaThucDon = 2;
                if (rdGroup.Checked)
                {
                    _cls.bool_GiaThucDon_Loai = 1;
                    _cls.Prices = grid.GetAcceptChanges();
                }
                else
                {
                    _cls.bool_GiaThucDon_Loai = 2;
                    _cls.Prices = l_Items1.GetData();
                }
            }

            _cls.OnUpdate();
        }
    }
}
